package com.gopay.common.util.xml.xmlutils.read.impl;

import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.gopay.common.util.xml.xmlutils.XmlProFileTemplateCheck;
import com.gopay.common.util.xml.xmlutils.read.XmlProFileReader;


/**
 * 
 * @author liujy
 * 
 */
public class XmlProFileReaderImpl implements XmlProFileReader {

	/**
	 * 将多个XML文件合并为一个XML以Document返回
	 * 
	 * @param documents
	 * @return
	 * @throws DocumentException
	 */
	@Override
    @SuppressWarnings("unchecked")
	public Document merge(Document[] documents) throws Exception {
		if (documents == null || documents.length == 0) {
			throw new IllegalArgumentException("documents");
		}
		String form_xml = "<messages></messages>";
		Document newDocument = DocumentHelper.parseText(form_xml);
		String xpath = "/messages";
		Document document = null;
		Element element = null;
		for (int i = 0; i < documents.length; i++) {
			document = documents[i];
			if (document != null) {
				List<Element> elements = document.selectNodes(xpath);
				if (elements != null && elements.size() > 0) {
					for (Iterator<Element> it = elements.iterator(); it
							.hasNext();) {
						element = (Element) it.next();
						newDocument.getRootElement().appendContent(element);
					}
				}
			}
		}
		// 模板合规检查待完成
		XmlProFileTemplateCheck.isValidatorCheck(newDocument);
		return newDocument;
	}

	/**
	 * 将定位的formPath路径转成Document数组
	 * 
	 * @param formPath
	 *            文件路径
	 * @return
	 * @throws DocumentException
	 */
	@Override
    public Document[] read(List<String> formPath) throws Exception {
		if (formPath == null || formPath.size() == 0) {
			throw new IllegalArgumentException("formPath: " + formPath);
		}
		Document[] documents = new Document[formPath.size()];
		SAXReader reader = new SAXReader();
		Document document = null;
		String file = null;
		String filePath = null;
		int i = 0;
		for (Iterator<String> it = formPath.iterator(); it.hasNext();) {
			file = (String) it.next();
			filePath = getFilePath(file);
			try {
				document = reader.read(filePath);
			} catch (DocumentException e) {
				throw e;
			}
			documents[i] = document;
			i++;
		}
		return documents;
	}

	/**
	 * 获取formPath路径转成Document
	 * 
	 * @param formPath
	 *            文件路径
	 * @return
	 * @throws DocumentException
	 */
	@Override
    public Document readParent(String formPath) throws Exception {
		if (formPath == null || formPath.length() == 0) {
			throw new IllegalArgumentException("formPath: " + formPath);
		}
		SAXReader reader = new SAXReader();
		String filePath = getFilePath(formPath);
		Document document = null;
		try {
			document = reader.read(filePath);
		} catch (DocumentException e) {
			throw e;
		}
		return document;
	}

	private String getFilePath(String file) {
		String path = this.getClass().getResource("/").toString();
		String fullPath = path + file;
		return fullPath;
	}
}
